package org.sixtynine.stock.controller.front;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.sixtynine.stock.Constants.ChartType;
import org.sixtynine.stock.Constants.CommonData;
import org.sixtynine.stock.criteria.StockSearchCriteria;
import org.sixtynine.stock.entity.BaseEntity;
import org.sixtynine.stock.model.ColData;
import org.sixtynine.stock.model.ColModel;
import org.sixtynine.stock.model.GridData;
import org.sixtynine.stock.service.GenericService;
import org.sixtynine.stock.service.content.DataManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Thilanka
 * 
 */
@Controller
public class StockScreenerController extends AbstractGridController {

	@Autowired
	private GenericService genericService;

	@Autowired
	DataManagerService dataManagerService;

	@RequestMapping(value = "/stockscreener/main")
	public ModelAndView showResults() {
		ModelAndView modelAndView = new ModelAndView("/stockscreener/main");

		StockSearchCriteria criteria = new StockSearchCriteria();
		modelAndView.addObject("criteria", criteria);
		showBannar(modelAndView);
		return modelAndView;
	}

	@RequestMapping(value = "/stockscreener/criteria/technical")
	public ModelAndView showTechnicalCriteria() {
		ModelAndView modelAndView = new ModelAndView(
				"/stockscreener/criteria/technical");

		StockSearchCriteria criteria = new StockSearchCriteria();
		modelAndView.addObject("criteria", criteria);

		return modelAndView;
	}

	@RequestMapping(value = "/stockscreener/criteria/fundamantal")
	public ModelAndView showFundamantalCriteria() {
		ModelAndView modelAndView = new ModelAndView(
				"/stockscreener/criteria/fundamantal");

		StockSearchCriteria criteria = new StockSearchCriteria();

		List<BaseEntity> sectors = getAllSectors();
		modelAndView.addObject("sectorsMap", sectors);

		modelAndView.addObject("criteria", criteria);

		return modelAndView;
	}

	@RequestMapping(value = "/stockscreener/criteria/descriptive")
	public ModelAndView showDescriptiveCriteria() {
		ModelAndView modelAndView = new ModelAndView(
				"/stockscreener/criteria/descriptive");

		StockSearchCriteria criteria = new StockSearchCriteria();

		List<BaseEntity> sectors = getAllSectors();
		modelAndView.addObject("sectorsMap", sectors);

		modelAndView.addObject("criteria", criteria);

		return modelAndView;
	}

	@RequestMapping(value = "/stockscreener/criteria/most_use")
	public ModelAndView showMostUseCriteria() {
		ModelAndView modelAndView = new ModelAndView(
				"/stockscreener/criteria/most_use");

		StockSearchCriteria criteria = new StockSearchCriteria();

		List<BaseEntity> sectors = getAllSectors();
		modelAndView.addObject("sectorsMap", sectors);

		modelAndView.addObject("criteria", criteria);
		showBannar(modelAndView);
		return modelAndView;
	}

	@RequestMapping(value = "/stockscreener/criteria/all")
	public ModelAndView showAllCriteria(
			@ModelAttribute StockSearchCriteria criteria) {
		ModelAndView modelAndView = new ModelAndView(
				"/stockscreener/criteria/all");

		List<BaseEntity> sectors = getAllSectors();
		modelAndView.addObject("sectorsMap", sectors);

		modelAndView.addObject("criteria", criteria);

		return modelAndView;
	}

	@RequestMapping(value = "/stockscreener/screener")
	public ModelAndView showScreener() {
		ModelAndView modelAndView = new ModelAndView("/stockscreener/screener");
		StockSearchCriteria criteria = new StockSearchCriteria();
		modelAndView.addObject("criteria", criteria);

		// add company list
		List<BaseEntity> compaies = dataManagerService
				.getData(CommonData.COMPANY.toString());
		modelAndView.addObject("companyList", compaies);
		showBannar(modelAndView);
		return modelAndView;
	}

	@RequestMapping(value = "/stockscreener/finance")
	public ModelAndView showFinanceData() {
		ModelAndView modelAndView = new ModelAndView("/stockscreener/finance");

		return modelAndView;
	}

	@RequestMapping(value = "/stockscreener/snapshot")
	public ModelAndView showSnapShot() {
		ModelAndView modelAndView = new ModelAndView("/stockscreener/snapshot");

		return modelAndView;
	}

	@RequestMapping(value = "/stockscreener/list", method = RequestMethod.GET)
	@ResponseBody
	public GridData getData(
			@RequestParam(value = "page", required = false, defaultValue = "1") int page,
			@RequestParam(value = "rows", required = false, defaultValue = "20") int rows,
			@RequestParam(value = "sidx", required = false) String sidx,
			@RequestParam(value = "sord", required = false) String sord,
			@RequestParam(value = "search", required = false) String search,
			HttpServletRequest request) {

		return getGridData(null, page, rows, sidx, sord, search, request);
	}

	@RequestMapping(value = "/stockscreener/colData", method = RequestMethod.GET)
	@ResponseBody
	public ColData getColumnData() {
		return getColData();
	}

	@RequestMapping(value = "/stockscreener/search", method = RequestMethod.POST)
	@ResponseBody
	public GridData getData(
			@RequestParam(value = "page", required = false, defaultValue = "1") int page,
			@RequestParam(value = "rows", required = false) Integer rows,
			@RequestParam(value = "sidx", required = false) String sidx,
			@RequestParam(value = "sord", required = false) String sord,
			@RequestParam(value = "search", required = false) String search,
			@ModelAttribute StockSearchCriteria criteria,
			HttpServletRequest request) {

		criteria.setChartType(ChartType.GENERAL);
		return getGridData(criteria, page, rows, sidx, sord, search, request);
	}

	@Override
	public ColData getColData() {
		ColData colData = new ColData();
		// col data
		List<ColModel> colModels = new ArrayList<ColModel>();

		colModels.add(new ColModel("ticker"));
		colModels.add(new ColModel("companyName"));
		colModels.add(new ColModel("index"));
		colModels.add(new ColModel("sma10"));

		colData.setColModel(colModels);

		List<String> colNames = new ArrayList<String>();
		colNames.add("Ticker");
		colNames.add("Company Name");
		colNames.add("Index");
		colNames.add("SMA 10");
		colData.setColNames(colNames);

		return colData;
	}

	/**
	 * @return sectors and sub sectors
	 */
	public List<BaseEntity> getAllSectors() {
		List<BaseEntity> sectorList = new ArrayList<BaseEntity>();
		List<BaseEntity> sectors = dataManagerService.getData(CommonData.SECTOR
				.toString());
		sectorList.addAll(sectors);

		List<BaseEntity> subSectors = dataManagerService
				.getData(CommonData.SUB_SECTOR.toString());
		sectorList.addAll(subSectors);

		return sectorList;
	}

}
